package com.handu.luna.core.domain.advertisement;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import com.handu.luna.core.domain.BaseRepository;



public interface AdvertiseListRepository extends JpaRepository<AdvertiseList,Long>, BaseRepository<AdvertiseList>,JpaSpecificationExecutor<AdvertiseList>{
 
	@Query("from AdvertiseList a  order by a.adPositionId desc ")
	Page<AdvertiseList> findAll(Pageable pageable);
	
	/**
	 * 点击广告位，列出相对应的广告列表
	 * @param id
	 * @param pageable
	 * @return
	 */
	
	@Query("from AdvertiseList a where a.adPositionId=:id")
	Page<AdvertiseList> findAll(@Param("id") Long id,Pageable pageable);
	
	/**
	 * 当删除广告位的时候，判断该广告位下是否有广告，若有，则不允许删除
	 * @param id
	 * @return
	 */
	
	@Query("from AdvertiseList a where a.adPositionId=:id")
	List<AdvertiseList> findAll(@Param("id") Long id);
	
	/**
	 * 前端获得相应的广告列表
	 */
	@Query("from AdvertiseList a where a.adPositionId=:id and a.startDate<= CURRENT_TIMESTAMP and a.endDate>= CURRENT_TIMESTAMP and a.displayed = true")
	List<AdvertiseList> findAllFront(@Param("id") Long id );
	
	@Query("from AdvertiseList a where a.adPositionId=:id and a.displayed=:displayed order by sortOrder asc")
	List<AdvertiseList> findByAdPositionId(@Param("id") Long id,@Param("displayed")boolean displayed);
	
	
	@Query("from AdvertiseList a where a.adPositionId = (select ap.id from AdPositionId ap where ap.adPositionSymbol = :symbol) and a.displayed=true and a.endDate >= CURRENT_TIMESTAMP and a.startDate <= CURRENT_TIMESTAMP order by a.sortOrder asc")
	Page<AdvertiseList> findByAdPositionSymbol4Portal(@Param("symbol") String symbol,Pageable pageable);
	/**
	 *-----------------------------------------
	 *  @Function 获取未过期的、能够显示的广告 专题页使用
	 *  @Params 广告id
	 *  @return List<AdvertiseList> 
	 *  @date  2014年9月5日 下午2:47:01
	 *-----------------------------------------
	 */
	@Query("from AdvertiseList a where a.adPositionId=:id and a.displayed=true and a.endDate >= CURRENT_TIMESTAMP and a.startDate <= CURRENT_TIMESTAMP order by a.sortOrder asc")
	List<AdvertiseList> findByAdPositionIdForPortal(@Param("id") Long id);
}
